package zeno.project.springboot.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import zeno.project.springboot.dao.UserMapper;
import zeno.project.springboot.entity.User;
import zeno.project.springboot.listener.customize.MyEvent;
import zeno.project.springboot.service.UserService;

import javax.annotation.Resource;
import java.util.Set;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Resource
    private ApplicationContext applicationContext;

    @Override
    public User getUserByName(String username) {
        return userMapper.getUserByName(username);
    }

    @Override
    public User getUser() {
        return new User(1, "首页数据", "123456");
    }

    /**
     * 发布事件
     * @return
     */
    public User getUser2() {
        User user = new User(1, "倪升武", "123456");
        // 发布事件
        MyEvent event = new MyEvent(this, user);
        applicationContext.publishEvent(event);
        return user;
    }

    @Override
    public Set<String> getRoles(String username) {
        return userMapper.getRoles(username);
    }

    @Override
    public Set<String> getPermissions(String username) {
        return userMapper.getPermissions(username);
    }

    @Override
    public User getByUsername(String username) {
        return userMapper.getUserByName(username);
    }
}
